package hos.table.holder;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import hos.table.BaseTableAdapter;
import hos.table.model.Cell;
import hos.table.model.ColumnHeader;
import hos.table.model.RowHeader;

/**
 * <p>Title: DataHolder </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2022/7/6 22:55
 */
public interface DataHolder<T> {

    void onBindViewHolder(@NonNull BaseTableAdapter adapter, @NonNull View itemView, @Nullable T model, int columnPosition, int rowPosition);


    void onConvert(@NonNull BaseTableAdapter adapter, @NonNull View itemView, @Nullable T model, int columnPosition, int rowPosition);

    default void layoutParams(@NonNull BaseTableAdapter adapter, @NonNull View itemView, @Nullable T model, int columnPosition, int rowPosition){
        OnTableSizeListener listener = adapter.getOnTableSizeListener();
        if (listener == null) {
            return;
        }
        ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
        if (layoutParams == null) {
            layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        if (model instanceof ColumnHeader) {
            // 列标题
            layoutParams.height = listener.columnHeaderHeight();
            layoutParams.width = listener.columnHeaderWidth(columnPosition);
        } else if (model instanceof RowHeader) {
            // 行标题
            layoutParams.height = listener.rowHeight(rowPosition);
            layoutParams.width = listener.rowHeaderWidth();
        } else if (model instanceof Cell){
            // 内容
            layoutParams.height = listener.rowHeight(rowPosition);
            layoutParams.width = listener.columnHeaderWidth(columnPosition);
        }
        itemView.setLayoutParams(layoutParams);
    }
}
